package com.ymatou.iapi.quickturnover.testcase;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.filefilter.AndFileFilter;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.quickturnover.parameter.GetSellerAdvanceInfoReqBean;
import com.ymatou.iapi.quickturnover.parameter.resp.GetSellerAdvanceInfoResp;
import com.ymatou.iapi.quickturnover.service.GetSellerAdvanceInfoReqCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.model.AdvanceOrder;
import com.ymttest.database.sqlwapper.AdvanceOrderDefaultWapper;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.constant.ECode;
/**
*获取提前放款用户汇总信息详情接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetSellerAdvanceInfoReq{
	private static GetSellerAdvanceInfoReqBean getselleradvanceinforeqBean;
	private static GetSellerAdvanceInfoReqCall getselleradvanceinforeqCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取提前放款用户汇总信息详情接口");
	}

	@Before
	public void caseUp() {
		getselleradvanceinforeqBean=new GetSellerAdvanceInfoReqBean();
		getselleradvanceinforeqCall=new GetSellerAdvanceInfoReqCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getselleradvanceinforeqCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
/*	grantedAmount	当前买手可预支金额(订单授信总金额)
	advancedAmount 当前买手预支中的金额(所有未冲销金额,包含冲销失败金额)
	unWrittenOffAmount 当前买手冲销失败金额(确收、投诉、退款后的需冲销而没有冲销的金额)*/
		
/*	    *//**
     * 创建
     *//*
    CREATE(1),
    
    *//**
     * 授信
     *//*
    GRANT_CREDIT(2),
    *//**
     * 已预支
     *//*
    ADVANCED(3),
    *//**
     * 冲销中
     *//*
    WRITE_OFF(4),
    *//**
     * 已冲销
     *//*
    WRITTEN_OFF(5);*/

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerAdvanceInfoReq_001() {
		Logger.start(true,"获取提前放款用户汇总信息详情");
		try {
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			getselleradvanceinforeqBean.setSellerId(sellerId);
			getselleradvanceinforeqCall.setData(getselleradvanceinforeqBean);
			getselleradvanceinforeqCall.callService();			
			
			Logger.verifyEquals(ECode.SUCCESS, getselleradvanceinforeqCall.getCode(), "验证Code");		

			AdvanceOrderDefaultWapper advanceOrderDefaultWapper=new AdvanceOrderDefaultWapper();	
			//advance_order
	        Map advance = new HashMap();  
	        advance.put("seller_id", sellerId);
	        List<AdvanceOrder> advanceOrderList=advanceOrderDefaultWapper.selectAdvanceOrder(advance);
	       
	        //可预支金额
	         BigDecimal can_advanced_amount =advanceOrderList.stream().filter(advanceOrder->advanceOrder.getAdvance_status()==2).map(advanceOrder->advanceOrder.getCan_advance_amount())
	        		.reduce(BigDecimal.ZERO, BigDecimal::add);
	       //已预支金额
	         BigDecimal advanced_amount =advanceOrderList.stream().filter(advanceOrder->advanceOrder.getAdvance_status()!=5 ).map(advanceOrder->advanceOrder.getAdvanced_amount())
		        		.reduce(BigDecimal.ZERO, BigDecimal::add);
	         // 已销账金额
	         BigDecimal writtenoff_amount =advanceOrderList.stream().filter(advanceOrder->advanceOrder.getAdvance_status()!=5 ).map(advanceOrder->advanceOrder.getWriteoff_fail_amount())
		        		.reduce(BigDecimal.ZERO, BigDecimal::add);
	         
	        GetSellerAdvanceInfoResp sellerAdvanceInfoResp=getselleradvanceinforeqCall.getSellerAdvanceInfo();

			Logger.verifyEquals(can_advanced_amount.setScale(2), sellerAdvanceInfoResp.getGrantedAmount().setScale(2), "验证grantedAmount");		
			Logger.verifyEquals(advanced_amount.setScale(2), sellerAdvanceInfoResp.getAdvancedAmount().setScale(2), "验证advanced_amount");		
			Logger.verifyEquals(writtenoff_amount.setScale(2), sellerAdvanceInfoResp.getUnWrittenOffAmount().setScale(2), "验证writtenoff_amount");		
			Logger.verifyEquals(sellerId, sellerAdvanceInfoResp.getSellerId(), "验证sellerId");		
			Logger.verifyEquals(true, getselleradvanceinforeqCall.getData().getBoolean("success"), "验证success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetSellerAdvanceInfoReq_002() {
		Logger.start(false,"sellerid 不存在");
		try {
			int sellerId = 11111;
			getselleradvanceinforeqBean.setSellerId(sellerId);
			getselleradvanceinforeqCall.setData(getselleradvanceinforeqBean);
			getselleradvanceinforeqCall.callService();			
			
			Logger.verifyEquals(ECode.SUCCESS, getselleradvanceinforeqCall.getCode(), "验证Code");		

			AdvanceOrderDefaultWapper advanceOrderDefaultWapper=new AdvanceOrderDefaultWapper();	
			//advance_order
	        Map advance = new HashMap();  
	        advance.put("seller_id", sellerId);
	        List<AdvanceOrder> advanceOrderList=advanceOrderDefaultWapper.selectAdvanceOrder(advance);
	       
	        //可预支金额
	         BigDecimal can_advanced_amount =advanceOrderList.stream().filter(advanceOrder->advanceOrder.getAdvance_status()==2).map(advanceOrder->advanceOrder.getCan_advance_amount())
	        		.reduce(BigDecimal.ZERO, BigDecimal::add);
	       //已预支金额
	         BigDecimal advanced_amount =advanceOrderList.stream().filter(advanceOrder->advanceOrder.getAdvance_status()!=5 ).map(advanceOrder->advanceOrder.getAdvanced_amount())
		        		.reduce(BigDecimal.ZERO, BigDecimal::add);
	         // 已销账金额
	         BigDecimal writtenoff_amount =advanceOrderList.stream().filter(advanceOrder->advanceOrder.getAdvance_status()!=5 ).map(advanceOrder->advanceOrder.getWriteoff_fail_amount())
		        		.reduce(BigDecimal.ZERO, BigDecimal::add);
	         
	        GetSellerAdvanceInfoResp sellerAdvanceInfoResp=getselleradvanceinforeqCall.getSellerAdvanceInfo();

			Logger.verifyEquals(can_advanced_amount.setScale(2), sellerAdvanceInfoResp.getGrantedAmount().setScale(2), "验证grantedAmount");		
			Logger.verifyEquals(advanced_amount.setScale(2), sellerAdvanceInfoResp.getAdvancedAmount().setScale(2), "验证advanced_amount");		
			Logger.verifyEquals(writtenoff_amount.setScale(2), sellerAdvanceInfoResp.getUnWrittenOffAmount().setScale(2), "验证writtenoff_amount");		
			Logger.verifyEquals(sellerId, sellerAdvanceInfoResp.getSellerId(), "验证sellerId");		
			Logger.verifyEquals(true, getselleradvanceinforeqCall.getData().getBoolean("success"), "验证success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetSellerAdvanceInfoReq_003() {
		Logger.start(false,"sellerid 为null");
		try {
			int sellerId = 11111;
			//getselleradvanceinforeqBean.setSellerId(sellerId);
			getselleradvanceinforeqCall.setData(getselleradvanceinforeqBean);
			getselleradvanceinforeqCall.callService();			
			
			Logger.verifyEquals(ECode.ERROR400, getselleradvanceinforeqCall.getCode(), "验证Code");		
			Logger.verifyEquals("sellerId:卖家ID不能为0", getselleradvanceinforeqCall.getString("msg"), "验证Code");		
			Logger.verifyEquals(false, getselleradvanceinforeqCall.getData().getBoolean("success"), "验证success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}